<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="名称"
        clearable
        style="width: 180px;"
      />  <el-select
        v-model="listQuery.typeId"
        placeholder="请选择接口类型"
        clearable
        style="width: 180px;"
      >
        <el-option
          v-for="item in listApitypeCombo"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <router-link :to="{path:`/sys/api/add`}">
        <el-button
          v-if="isP('SysApiAdd')"
          class=" light-primary-btn"
          style="margin-left: 10px;"
          type="primary"
          icon="el-icon-plus"
        >
          新增
        </el-button>
      </router-link>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        prop="name"
        label="名称"
        width="100"
      /> <el-table-column
        prop="typeName"
        label="接口类型"
        width="120"
      />
      <el-table-column
        prop="url"
        label="url"
        width="240"
      />
      <el-table-column
        prop="requestType"
        label="请求方式"
        width="180"
        :formatter="requestTypeFormatter"
      />

      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button v-if="isP('SysApiUpdate')" style="margin-left: 10px" type="primary" size="mini" @click="handleUpdate(scope)">修改</el-button>
          <el-button v-if="isP('SysApiDelete')" style="margin-left: 10px" type="danger" size="mini" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="listSysApi"
    />
  </div>
</template>

<script>
import { listSysApi, deleteSysApi } from '@/api/sys/api'
import { listApitypeCombo } from '@/api/common/common'
import Pagination from '@/components/Pagination'
import msgUtil from '@/utils/msgUtil'
import PageUtil from '@/utils/PageUtil'
import ComboConst from '@/utils/ComboConst'
export default {
  components: { Pagination },
  data() {
    return {
      listApitypeCombo: null, // 下拉框
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: JSON.parse(localStorage.getItem('SysApiQuery')) || {
        name: '',
        typeId: '',
        page: 1,
        limit: 20
      }
    }
  },
  created() {
    this.listSysApi()
    this.listSysApitype()
  },
  methods: {
    requestTypeFormatter(row) {
      return PageUtil.formatValue(ComboConst.requestTypeOptions, row.requestType)
    },
    listSysApitype() {
      listApitypeCombo({}).then(response => {
        this.listApitypeCombo = response.data
      })
    },
    listSysApi() {
      this.listLoading = true
      listSysApi(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      })
      localStorage.setItem('SysApiQuery', JSON.stringify(this.listQuery))
    },
    // 按钮查询
    handleFilter() {
      this.listQuery.page = 1
      this.listSysApi()
    },
    // 新增
    handleAdd() {
      // this.$router.push({ path: '/api/add' }) // 带参跳转
    },
    // 删除
    handleDelete({ $index, row }) {
      msgUtil.confirm('确认删除接口?').then(async() => {
        await deleteSysApi({ id: row.id })
        msgUtil.success2('删除成功!')
        this.listSysApi()
      })
    },
    // 处理修改
    handleUpdate({ $index, row }) {
      this.$router.push({
        path: '/sys/api/update',
        query: { id: row.id }
      })
    }
  }
}
</script>
